package com.sunny.core.controller;

import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sunny.core.bean.PersonInfo;
import com.sunny.core.bean.UserInfo;
import com.sunny.core.service.IPersonInfoService;
import com.sunny.core.utils.CommonResult;
import com.sunny.core.utils.CommonStringUtils;
import com.sunny.core.utils.Constant;

@Controller
@RequestMapping("/person")
public class PersonController {

	@Resource(name = "personInfoService")
	private IPersonInfoService personInfoService;
	
	/**
	 *  人员查询
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/query", method = { RequestMethod.POST,
			RequestMethod.GET })
	public ModelAndView queryAll(HttpServletRequest request,
			HttpServletResponse response,Model m) throws Exception {

		PersonInfo personInfo = new PersonInfo();
		
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");

		int num = 1;
		int size = 10;
		if (pageNum != null && !"".equals(pageNum)) {
			num = Integer.parseInt(pageNum);
		}
		if (pageSize != null && !"".equals(pageSize)) {
			size = Integer.parseInt(pageSize);
		}
		String sortString = "id.desc";
		Order.formString(sortString);

		PageHelper.startPage(num, size);
		
		String searchType = request.getParameter("searchType");
		String searchValue = request.getParameter("searchValue");
		
//		if (StringUtils.isNotBlank(personName)) {
//
//		}		
		
		
		if (StringUtils.isNotBlank(searchType)) {
			if (searchType.equals("1")) {
				personInfo.setPersonName(searchValue);
			}
			if (searchType.equals("2")) {
				personInfo.setBirthday(searchValue);
			}
		}
		
		List<PersonInfo> personInfoList = personInfoService.query(personInfo);

		PageInfo<PersonInfo> pagehelper = new PageInfo<PersonInfo>(personInfoList);

		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		// 相当 于request的setAttribut，在jsp页面中通过personList取数据
		// modelAndView.addObject("personList", personList);
		modelAndView.addObject("pagehelper", pagehelper);

		m.addAttribute("personInfoList",personInfoList);
		m.addAttribute("total",pagehelper.getTotal());
		m.addAttribute("pageSize",pagehelper.getPageSize());
		m.addAttribute("pageNum",num);
		m.addAttribute("searchType",searchType);
		m.addAttribute("searchValue",searchValue);
		
		
		// 指定视图
		// 下边的路径，如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀，修改为
		// modelAndView.setViewName("/jsp/personList.jsp");
		// 上边的路径配置可以不在程序中指定jsp路径的前缀和jsp路径的后缀
		modelAndView.setViewName("person/person_info_list");
		return modelAndView;

	}
	
	
	@RequestMapping(value = "/delete", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public CommonResult delete(HttpServletRequest request,HttpSession session) throws Exception {	
		PersonInfo personInfo =  new PersonInfo();
		CommonResult commonResult = new CommonResult();
		String personPid = request.getParameter("personPid");
		personInfo.setPersonPid(personPid);
		personInfo.setIsdel(1);
		int result = personInfoService.updateByPrimaryKeySelective(personInfo);
		if (result == 1) {
			commonResult.setStatus(Constant.RESULT_SCUSSES_CODE);
			commonResult.setMsg(Constant.RESULT_DELSUC_DESC);
		} else {
			commonResult.setStatus(Constant.RESULT_ERROR_CODE);
			commonResult.setMsg(Constant.RESULT_DELERR_DESC);
		}
		return commonResult;
	}
	
	/**
	 *  跳到人员新增页面
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/toAdd", method = { RequestMethod.POST,
			RequestMethod.GET })
	public ModelAndView toAdd(HttpServletRequest request,
			HttpServletResponse response,Model m) throws Exception {

		ModelAndView modelAndView = new ModelAndView();
		
		// 相当 于request的setAttribut，在jsp页面中通过personList取数据
		// modelAndView.addObject("personList", personList);
		
		
		// 指定视图
		// 下边的路径，如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀，修改为
		// modelAndView.setViewName("/jsp/personList.jsp");
		// 上边的路径配置可以不在程序中指定jsp路径的前缀和jsp路径的后缀
		modelAndView.setViewName("person/person_info_add");
		return modelAndView;

	}
	
	@RequestMapping(value = "/add", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public CommonResult add(HttpServletRequest request,HttpSession session,PersonInfo personInfo) throws Exception {	
		CommonResult commonResult = new CommonResult();
		
        Enumeration<String> e = session.getAttributeNames();
        while (e.hasMoreElements()) {
            String s = e.nextElement();
            System.out.println(s + " == " + session.getAttribute(s));
        }
		
		UserInfo currentUser = (UserInfo) session.getAttribute("currentUser");
		personInfo.setPersonPid(CommonStringUtils.get32UUID());
		personInfo.setIsdel(new Integer(0));
		personInfo.setCreateTime(new Date());
		personInfo.setCreateUserid(currentUser.getUserPid());
		personInfo.setCreateUsername(currentUser.getRealName());
		
		int result = personInfoService.save(personInfo);
		if (result == 1) {
			commonResult.setStatus(Constant.RESULT_SCUSSES_CODE);
			commonResult.setMsg(Constant.RESULT_ADDSUC_DESC);
		} else {
			commonResult.setStatus(Constant.RESULT_ERROR_CODE);
			commonResult.setMsg(Constant.RESULT_ADDERR_DESC);
		}
		return commonResult;
	}
	
	
	/**
	 *  跳到人员新增页面
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/toEdit", method = { RequestMethod.POST,
			RequestMethod.GET })
	public ModelAndView toEdit(HttpServletRequest request,
			HttpServletResponse response,Model m,PersonInfo personInfo) throws Exception {

		ModelAndView modelAndView = new ModelAndView();
		
		if (personInfo != null && StringUtils.isNotBlank(personInfo.getPersonPid())) {
			PersonInfo entity = personInfoService.selectByPrimaryKey(personInfo);
			
			
			if (entity != null) {
				
				m.addAttribute("flag","edit");
				m.addAttribute("personInfo",entity);
				
				modelAndView.setViewName("person/person_info_add");
				return modelAndView;
			}
		}
		return null;
	}
	
	/**
	 * 根据person_info的主键修改person_info表的所有信息
	 * @param request
	 * @param session
	 * @param personInfo
	 * @return
	 * @throws Exception
	 */
			
	@RequestMapping(value = "/update", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public CommonResult update(HttpServletRequest request,HttpSession session,PersonInfo personInfo) throws Exception {	
		CommonResult commonResult = new CommonResult();
		
		commonResult.setStatus(Constant.RESULT_ERROR_CODE);
		commonResult.setMsg(Constant.RESULT_UPERR_DESC);
		
		if (personInfo != null && personInfo.getPersonPid() != null) {
			personInfo.setUpdateTime(new Date());
			
			int result = personInfoService.updateByPrimaryKeySelective(personInfo);
			
			if (result == 1) {
				commonResult.setStatus(Constant.RESULT_SCUSSES_CODE);
				commonResult.setMsg(Constant.RESULT_UPSUC_DESC);
			} else {
				commonResult.setStatus(Constant.RESULT_ERROR_CODE);
				commonResult.setMsg(Constant.RESULT_UPERR_DESC);
			}
		}

		return commonResult;
	}
	
	
	/**
	 *  跳到人员新增页面
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/toDetail", method = { RequestMethod.POST,
			RequestMethod.GET })
	public ModelAndView toDetail(HttpServletRequest request,
			HttpServletResponse response,Model m,PersonInfo personInfo) throws Exception {

		ModelAndView modelAndView = new ModelAndView();
		
		if (personInfo != null && StringUtils.isNotBlank(personInfo.getPersonPid())) {
			PersonInfo entity = personInfoService.selectByPrimaryKey(personInfo);
			
			
			if (entity != null) {
				
				m.addAttribute("flag","detail");
				m.addAttribute("personInfo",entity);
				
				modelAndView.setViewName("person/person_info_add");
				return modelAndView;
			}
		}
		return null;
	}	
}
